//
//  AIDiaryReplayBean.swift
//  imprint
//
//  Created by 小柠檬🍋 on 2025/3/11.
//
/**
 * 来信 - minmax
 */
import Foundation
import SwiftUI
class AIWeeklyLetterMinmaxBean : Codable {
    
    var tokens_to_generate: Int64 = 1034
    var model = "abab6.5s-chat"
    var temperature: Double = 0.9
    var top_p: Double = 0.95
    var messages: [Messages]
    var reply_constraints: ReplyConstraintsc //回复格式
    var bot_setting:[BotSetting] //对每一个机器人的设定
    
    
    init(content : String, time: TimeInterval, botRoleBean: RoleBean) {
        let userRoleBean = RoleBean.get(userManager.user, botRoleBean)
        self.reply_constraints = ReplyConstraintsc(aiRole: botRoleBean)
        self.bot_setting = [BotSetting(aiRole: botRoleBean)]
        self.messages = AIWeeklyLetterMinmaxBean.getMessagesList(content: content, time: time, botRoleBean: botRoleBean, userRoleBean: userRoleBean)
        
    }
    
    /**
     * 模型回复要求.
     */
    class ReplyConstraintsc : Codable{
        var sender_type = "BOT"
        var sender_name = ""  //指定回复的机器人名称。
        
        init(aiRole: RoleBean) {
            self.sender_name = aiRole.name ?? ""
        }
        
    }
    
    /**
     * 对每一个机器人的设定。
     * @param content 具体机器人的设定,长度影响接口性能。
     */
    class BotSetting : Codable {
        var bot_name: String
        var content: String
        init(aiRole: RoleBean) {
            bot_name = aiRole.name ?? ""
            content = RoleBean.getBotDesc(aiRole)
        }
    }
    
    class Messages: Codable {
        var sender_type = "USER"
        var text: String
        var sender_name: String
        
        init(_ content: String,_ botRoleBean: RoleBean) {
            var name = AiChatExt.getName2(botRoleBean)
            if name.isEmpty == true {
                name = "亲爱的"
            }
            self.text = content
            self.sender_name = name
        }
    }
    
    private static func getMessagesList(content : String, time: TimeInterval, botRoleBean: RoleBean,
                                               userRoleBean: RoleBean) ->[Messages] {
        var listMsg : [Messages] = []
        //用户人设
        let userDesc: String = RoleBean.getUserDesc(botRoleBean)
        listMsg.append(Messages(userDesc,botRoleBean))
        //添加时间
        let timeStr = TimeUtils.getDate(time: time, format: "yyyy-MM-dd HH:mm")
        listMsg.append(Messages("现在的时间是：\(timeStr),这条信息仅用于你理解，不用说出来。",botRoleBean))
        //添加内容
        let endStr = "为我写一封温暖的信，请以‘亲爱的\(botRoleBean.name2 ?? "")'作为开头，以'\(botRoleBean.name ?? "")'作为结尾,\(configManager.getConfig().getAiDescribe().letterDescribe ?? "")"
        let content2 = content.isEmpty ? endStr : "以下是我最近一周的日记中，比较重要的一部分:\(content)’。请根据我的日记内容，\(endStr)"
        listMsg.append(Messages(content2.formatAiChat(botRoleBean, time),userRoleBean))
        return listMsg
    }
    
    
}
